package com.xinqi.modules.homework.homework.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.common.mybatis.utils.Pages;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.homework.homework.convert.CourseHomeworkFileConvert;
import com.xinqi.modules.homework.homework.domain.CourseHomeworkFileEntity;
import com.xinqi.modules.homework.homework.dto.req.CourseHomeworkFileCreateDTO;
import com.xinqi.modules.homework.homework.dto.req.CourseHomeworkFileDeleteDTO;
import com.xinqi.modules.homework.homework.dto.req.CourseHomeworkFileQueryDTO;
import com.xinqi.modules.homework.homework.dto.req.CourseHomeworkFileUpdateDTO;
import com.xinqi.modules.homework.homework.dto.rsp.CourseHomeworkFileResultDTO;
import com.xinqi.modules.homework.homework.mapper.CourseHomeworkFileMapper;
import com.xinqi.modules.homework.homework.service.CourseHomeworkFileService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 作业提交文件记录表服务层实现
 *
 * @author: system
 * @date: 2023/03/08
 */
@Service("courseHomeworkFileService")
@RequiredArgsConstructor
public class CourseHomeworkFileServiceImpl extends BaseServiceImpl<CourseHomeworkFileMapper, CourseHomeworkFileEntity> implements CourseHomeworkFileService {

    private final CourseHomeworkFileConvert courseHomeworkFileConvert;

    /**
     * 分页查询 作业提交文件记录表数据
     */
    @Override
    @DS("slaver")
    public PageInfo<CourseHomeworkFileResultDTO> page(Param pageable, CourseHomeworkFileQueryDTO query) {
        return this.baseMapper.selectDtoPageInfo(Pages.page(pageable), wrapper(query), courseHomeworkFileConvert::convert);
    }

    /**
     * 查询 作业提交文件记录表数据
     */
    @Override
    public List<CourseHomeworkFileResultDTO> find(CourseHomeworkFileQueryDTO query) {
        return this.baseMapper.selectDtoList(wrapper(query), courseHomeworkFileConvert::convert);
    }

    /**
     * 查询 作业提交文件记录表数据根据记录ID
     */
    @Override
    public List<CourseHomeworkFileResultDTO> findByRecordId(Long recordId) {
        CourseHomeworkFileQueryDTO fileQueryDTO = new CourseHomeworkFileQueryDTO();
        fileQueryDTO.setRecordId(recordId);
        return this.find(fileQueryDTO);
    }

    private LambdaQueryWrapper<CourseHomeworkFileEntity> wrapper(CourseHomeworkFileQueryDTO query) {
        LambdaQueryWrapper<CourseHomeworkFileEntity> wrapper = Queries.lambda();
        if (!Objects.isNull(query.getId())) {
            Queries.accept(query.getId(), v -> wrapper.eq(CourseHomeworkFileEntity::getId, v));
        }
        if (!Objects.isNull(query.getRecordId())) {
            Queries.accept(query.getRecordId(), v -> wrapper.eq(CourseHomeworkFileEntity::getRecordId, v));
        }
        return wrapper;
    }

    /**
     * 根据id查询 作业提交文件记录表数据
     */
    @Override
    public CourseHomeworkFileResultDTO findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), MessageUtils.message("course.homework.id.not.blank"));
        return this.baseMapper.selectDtoById(id, courseHomeworkFileConvert::convert);
    }

    /**
     * 新增 作业提交文件记录表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(CourseHomeworkFileCreateDTO dto) {
        CourseHomeworkFileEntity entity = courseHomeworkFileConvert.create(dto);
        this.save(entity);
        return entity.getId();
    }

    /**
     * 根据id修改 作业提交文件记录表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateById(CourseHomeworkFileUpdateDTO dto) {
        CourseHomeworkFileEntity old = Optional.ofNullable(this.getById(dto.getId()))
                .orElseThrow(() -> CommonErrors.BAD_REQUEST.asException(MessageUtils.message("data.does.not.exist")));
        CourseHomeworkFileEntity entity = courseHomeworkFileConvert.update(dto);
        return this.updateById(entity);
    }

    /**
     * 批量修改 作业提交文件记录表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveBatch(Long recordId, List<CourseHomeworkFileCreateDTO> list) {
        List<CourseHomeworkFileEntity> entityList = new ArrayList<>();
        list.forEach(item -> {
            CourseHomeworkFileEntity entity = courseHomeworkFileConvert.create(item);
            entity.setRecordId(recordId);
            entityList.add(entity);
        });
        this.saveBatch(entityList);
        return true;
    }

    /**
     * 根据id删除 作业提交文件记录表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), MessageUtils.message("course.homework.id.not.blank"));
        return this.removeById(id);
    }

    /**
     * 根据id删除 作业提交文件记录表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(CourseHomeworkFileDeleteDTO dto) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(dto.getId()), MessageUtils.message("course.homework.id.not.blank"));
        return this.deleteById(dto.getId());
    }

}
